package com.starbucks.opencase.function;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.data.daFriendMessage;
import com.starbucks.opencase.data.daPath;
import com.starbucks.opencase.entity.entUserInfo;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMMark;
import com.starbucks.opencase.modualr.moFriendMessage;
import com.starbucks.opencase.tool.toolUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class funFriendMessage {
    @Autowired
    private funSend funSend;
    @Autowired
    private funSession funSession;
    @Autowired
    private toolUserInfo toolUserInfo;
    public long send(String uid, String to_uid, ObjectNode message) {
        moFriendMessage mo = new moFriendMessage();
        mo.setTimestamp(System.currentTimeMillis());
        mo.setMessage(message);
        mo.setSender(uid);
        mo.setRecipient(to_uid);
        daFriendMessage.message.put(mo.getTimestamp(), mo);
        ObjectMapper om = new ObjectMapper();
        //对方不在线
        if (!funSession.isOnlineOfUId(to_uid)) return mo.getTimestamp();
        //对方在线
        ObjectNode on = om.createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.friendMessage.ordinal());

        entUserInfo sender_info = toolUserInfo.queryInfo(uid);
        ObjectNode sender = om.createObjectNode();
        sender.put(enumCMKey.uid.name(), sender_info.getUid());
        sender.put(enumCMKey.nickname.name(), sender_info.getNickname());
        sender.put(enumCMKey.url.name(), daPath.getAvatar(sender_info.getGender() + ""));
        on.put(enumCMKey.sender.name(), sender);

        entUserInfo recipient_info = toolUserInfo.queryInfo(to_uid);
        ObjectNode recipient = om.createObjectNode();
        recipient.put(enumCMKey.uid.name(), recipient_info.getUid());
        recipient.put(enumCMKey.nickname.name(), recipient_info.getNickname());
        recipient.put(enumCMKey.url.name(), daPath.getAvatar(recipient_info.getGender() + ""));
        on.put(enumCMKey.recipient.name(), recipient);

        on.put(enumCMKey.message.name(), message);
        on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
        funSession.sendMessage(funSession.getIdOfUId(to_uid), on);
        return mo.getTimestamp();
    }

    /**
     * 获取申请好友但对方还没有处理该申请的消息
     * @param uid
     * @return
     */
    public List<moFriendMessage> queryApplyList(String uid){
        ArrayList<moFriendMessage> as=new ArrayList<>();
        for(moFriendMessage m:daFriendMessage.message.values()){
            if(m.getMessage().get(enumCMKey.type.name()).asInt()==1 && m.getSender().equals(uid))as.add(m);
        }
        return as;
    }

    /**
     * 获取好友申请己方没有处理的申请消息
     * @param uid
     * @return
     */
    public List<moFriendMessage> queryApplyFromList(String uid){
        ArrayList<moFriendMessage> as=new ArrayList<>();
        for(moFriendMessage m:daFriendMessage.message.values()){
            if(m.getMessage().get(enumCMKey.type.name()).asInt()==1 && m.getRecipient().equals(uid))as.add(m);
        }
        return as;
    }

    /**
     * 删除消息
     * @param mid
     */
    public void removeMessage(long mid){
        daFriendMessage.message.remove(mid);
    }
    public moFriendMessage getMessage(long mid){
        return daFriendMessage.message.get(mid);
    }
}
